/*
 * Copyright 2012 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudget.client.lists;

import java.util.List;

import com.google.gwt.event.shared.GwtEvent;

import finbudget.client.lists.GetListOfItems.BudgetListItem;
import finbudget.client.lists.GetListOfItems.ListType;

/**
 * This event is fired before the actual list is requested from the server.
 * The client have a chance to cancel event and provide it's own list of items
 * by handling this event and calling cancelRequest.
 *
 */
public class ListRequestedEvent extends GwtEvent<ListRequestedHandler> {
	public static Type<ListRequestedHandler> TYPE = new Type<ListRequestedHandler>();

	private final ListType listType;
	private boolean canceled;
	/**
	 * List of items in case the event is canceled.
	 */
	private List<BudgetListItem> items;

	public ListRequestedEvent(ListType listType) {
		this.listType = listType;
	}

	@Override
	public Type<ListRequestedHandler> getAssociatedType() {
		return TYPE;
	}

	@Override
	protected void dispatch(ListRequestedHandler handler) {
		if (handler != null) {
			handler.onListRequested(this);
		}
	}

	public ListType getListType() {
		return listType;
	}

	/**
	 * Prevent requesting list from the server and provide own list of items for requested list.
	 * @param items
	 */
	public void cancelRequest(List<BudgetListItem> items) {
		canceled = true;
		this.items = items;
	}

	public boolean isCanceled() {
		return canceled;
	}

	public List<BudgetListItem> getItems() {
		return items;
	}
}
